package com.mini.grpc.examples.interceptor;

import com.mini.grpc.client.ClientInterceptor;
import com.mini.grpc.server.ServerInterceptor;
import com.mini.grpc.client.ClientCall;
import com.mini.grpc.client.Channel;
import com.mini.grpc.client.CallOptions;
import com.mini.grpc.client.MiniGrpcChannel;
import com.mini.grpc.server.ServerCall;
import com.mini.grpc.server.ServerCallHandler;
import com.mini.grpc.common.MethodDescriptor;
import com.mini.grpc.common.Metadata;
import com.mini.grpc.common.Status;

import java.util.logging.Logger;

/**
 * 简化的日志拦截器示例
 */
public class LoggingInterceptor {
    private static final Logger logger = Logger.getLogger(LoggingInterceptor.class.getName());
    
    /**
     * 客户端日志拦截器
     */
    public static class ClientLoggingInterceptor implements ClientInterceptor {
        @Override
        public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
                MethodDescriptor<ReqT, RespT> method,
                CallOptions callOptions,
                MiniGrpcChannel next) {
            logger.info("Client call started: " + method.getFullMethodName());
            
            // 简化实现 - 实际应该包装ClientCall
            return next.newCall(method, callOptions);
        }
    }
    
    /**
     * 服务端日志拦截器
     */
    public static class ServerLoggingInterceptor implements ServerInterceptor {
        @Override
        public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
                ServerCall<ReqT, RespT> call,
                Metadata headers,
                ServerCallHandler<ReqT, RespT> next) {
            logger.info("Server call started: " + call.getMethodDescriptor().getFullMethodName());
            logger.info("Server call headers: " + headers);
            
            // 简化实现 - 实际应该包装ServerCall.Listener
            return next.startCall(call, headers);
        }
    }
} 